<?php
class EwmModel
{
    public static function madedir($filedir="")
    {
        if(!is_dir($filedir))
        {
                mkdir($filedir, 0777, true);
                chmod($filedir,0777);
        }
    }
    //针对投票生成的二维码
    public static function wl($url,$otherArr=array())
    {
        $id = $otherArr['id'];
        $uid = $otherArr['uid'];
        $rela_dir = "ewm/wl/".$uid;
        $filename = $id.".png";
        $pic = $rela_dir."/".$filename;
        $abs_filename = PICUP."/".$pic;
        $tf = file_exists($abs_filename);
         
        if($tf == false)
        {
            include_once ROOT.'/libs/phpqrcode/phpqrcode.php';
            $errorCorrectionLevel = 'L';//容错级别
            $matrixPointSize = 10;//生成图片大小 
    //        $filename = microtime(true);
    //        $tihuan = array('.'=>'');
    //        $filename = strtr($filename,$tihuan);
            self::madedir(PICUP."/".$rela_dir);
            QRcode::png($url, PICUP."/".$pic, $errorCorrectionLevel, $matrixPointSize, 2);   //二维码生成
            
            //开始生成logo的水印
//            $logo_dir = PICUP."/touxiang.png";//准备好的logo图片  
//            if(file_exists($logo_dir))
//            {
//                $QR = imagecreatefromstring(file_get_contents($abs_filename));   
//                $logo = imagecreatefromstring(file_get_contents($logo_dir)); 
//                $QR_width = imagesx($QR);//二维码图片宽度  
//                $QR_height = imagesy($QR);//二维码图片高度   
//                $logo_width = imagesx($logo);//logo图片宽度   
//                $logo_height = imagesy($logo);//logo图片高度   
//                $logo_qr_width = $QR_width / 4;   
//                $scale = $logo_width/$logo_qr_width;   
//                $logo_qr_height = $logo_height/$scale;   
//                $from_width = ($QR_width - $logo_qr_width) / 2;   
//                //重新组合图片并调整大小   
//                $tf2 = imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);   
//                if($tf2)
//                {
//                    imagepng($QR, $abs_filename);
//                }
//            }
        }
        
        return $pic;
    }
    
    //自己的二维码
    public static function ewm($uid,$url)
    {
        include_once ROOT.'/lib/phpqrcode/phpqrcode.php';
        $Ym = date("Ym");
        self::madedir(PICUP."/ewm/".$Ym);
        $filename = "ewm/".$Ym."/self_".$uid.".png";
        $tf = file_exists(PICUP."/".$filename);
        if(!$tf)
        {
            $errorCorrectionLevel = 'L';//容错级别 
            $matrixPointSize = 8;//生成图片大小 
            //生成二维码图片 
            QRcode::png($url, PICUP."/".$filename, $errorCorrectionLevel, $matrixPointSize, 2); 
//            $logo = PICUP."/logo_ewm.png";
//            $tf2 = file_exists($logo);
//            if($tf2)
//            {
//                $QR = imagecreatefromstring(file_get_contents(PICUP."/".$filename)); 
//                $logo = imagecreatefromstring(file_get_contents($logo)); 
//                $QR_width = imagesx($QR);//二维码图片宽度 
//                $QR_height = imagesy($QR);//二维码图片高度 
//                $logo_width = imagesx($logo);//logo图片宽度 
//                $logo_height = imagesy($logo);//logo图片高度 
//                $logo_qr_width = $QR_width / 5; 
//                $scale = $logo_width/$logo_qr_width; 
//                $logo_qr_height = $logo_height/$scale; 
//                $from_width = ($QR_width - $logo_qr_width) / 2; 
//                //重新组合图片并调整大小 
//                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); 
//                imagepng($QR, PICUP."/".$filename); 
//                
//            }
            return $filename;
        }else
        {
            return $filename;
        }
    }
    //创建我的我二维码图片：可用来分享
    public static function cremyewmpic($uid,$name,$headimg)
    {
        if(empty($headimg))
        {
            $headimg = 'logo_ewm.png';
        }
        
        $head_pic = PICUP.'/'.$headimg; //头像

        //判断文件是否存在
        if(!file_exists($head_pic))$head_pic = PICUP.'/logo_ewm.png';

        $bg = PICUP."/bg_base.jpg";
        $quan = PICUP.'/quan.png';
        
        $ewm = self::ewm($uid, "http://houtai.syxhn.cn/m_login/?id=$uid");//二维码图片 (小正方形)
        $path_ewm = PICUP."/".$ewm;
        
//        $img_bg = imagecreatefromjpeg($bg);
        $img_bg = imagecreatefromstring(file_get_contents($bg)); 
//        $image_ewm = imagecreatefrompng($path_ewm);//二维码图片
        //合并头像
        $image_head = imagecreatefromstring(file_get_contents($head_pic)); //创建一个资源
        list($width, $height) = getimagesize($head_pic);
        imagecopyresized($img_bg, $image_head, 195,165, 0, 0, 150, 150, $width, $height); //把头像放到背景
//        imagecopyresized($img_bg, $image_head, 194,166, 0, 0, 150, 150, $width, $height);
        
        //合并圆圈
        $image_quan = imagecreatefromstring(file_get_contents($quan)); 
        list($width, $height) = getimagesize($quan);
        imagecopyresized($img_bg, $image_quan, 145,145, 0, 0, $width, $height, $width, $height);
        
        //合并二维码
        $image_ewm = imagecreatefromstring(file_get_contents($path_ewm)); 
        list($width, $height) = getimagesize($path_ewm);
        imagecopyresized($img_bg, $image_ewm, 180,600, 0, 0, 160, 160, $width, $height);
        
        //写字 
        $textcolor=imagecolorallocate($img_bg,0, 0, 0);
        imagettftext($img_bg, 20, 0, 120, 800, $textcolor, PICUP.'/msyhl.ttc', '扫描二维码，加入小黄牛');
        
        $name_len = self::getstrlen($name);
        $name_len2 = $name_len / 2;
        $start = 235;
        $start = $start - $name_len2;
        imagettftext($img_bg, 15, 0, $start , 350, $textcolor, PICUP.'/msyhl.ttc', 'hi,我是'.$name);
        
        
        //生成图片
//        header("content-type: image/jpeg"); 
//        imagejpeg($img_bg,null,100); 
        self::madedir(PICUP."/ewm/".date("Ym"));
        $pic = 'ewm/'.date("Ym").'/'.$uid.'_'.time().'.jpg';
        imagejpeg($img_bg,PICUP.'/'.$pic,100); 
        
        imagedestroy($img_bg);
        imagedestroy($image_ewm); 
        
        $backarr = array(
            'ewm'=>$ewm,
            'ewm_pic'=>$pic
        );
        return $backarr;
    }
    public static function getstrlen($name)
    {
        $_name = $name;
        $hanzi_num = 0; 
        $preg = "/[\x{4e00}-\x{9fa5}]+/u"; 
        preg_match_all($preg,$name,$matches);
        if($matches[0])
        {
            foreach($matches[0] as $key=>$value)
            {
                $len = mb_strlen($value);
                $hanzi_num = $hanzi_num + $len; 
                $_name = self::str_replace_once($value, "", $_name); 
            }
        } 
         
        $en_num = strlen($_name);
        $len = $hanzi_num * 20 + $en_num * 10; 
        return $len;
    }
 
    public static function str_replace_once($needle, $replace, $haystack) 
    {
        // Looks for the first occurence of $needle in $haystack
        // and replaces it with $replace.
        $pos = strpos($haystack, $needle);
        if ($pos === false) {
            // Nothing found
            return $haystack;
        }
        return substr_replace($haystack, $replace, $pos, strlen($needle));
    }
    
}
?>